package cn.sdormitory.controller.knowledgebase;

import cn.sdormitory.common.api.CommonResult;
import cn.sdormitory.knowledgebase.dto.RuleRegulationDto;
import cn.sdormitory.knowledgebase.pojo.RuleRegulation;
import cn.sdormitory.knowledgebase.service.RuleRegulationService;
import cn.sdormitory.knowledgebase.vo.RuleRegulationVo;
import cn.sdormitory.tencent.service.TencentCosFileService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@Api(tags = "Oss-aliyun => 规章制度")
@RequestMapping("/regulatory/regulation")
public class    regulationController {
    @Autowired
    private RuleRegulationService ruleRegulationService;

    @Autowired
    private TencentCosFileService cosFileService;

    /**
     * 获取规章制度列表
     * @param pageNum
     * @param pageSize
     * @param title
     * @param type
     * @return
     */
    @GetMapping("/getList")
    public CommonResult getList(
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(value = "title", required = false) String title,
            @RequestParam(value = "type", required = false) Integer type
    ) {
        IPage<RuleRegulationVo> ruleRegulationList = ruleRegulationService.getRuleRegulationList(pageNum,pageSize,title,type);
        return CommonResult.success(ruleRegulationList);
    }

    /**
     * 新增规章制度
     * @param ruleRegulationDto
     * @return
     */
    @PostMapping("/create")
    public CommonResult create(@RequestBody RuleRegulationDto ruleRegulationDto) {
        int count = ruleRegulationService.addRuleRegulation(ruleRegulationDto);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    /**
     * 根据id获取规章制度信息
     * @param id
     * @return
     */
    @GetMapping("/getInfo/{id}")
    public CommonResult getInfo(@PathVariable("id") Integer id) {
        RuleRegulation ruleRegulation = ruleRegulationService.getRuleRegulationById(id);
        return CommonResult.success(ruleRegulation);
    }

    /**
     * 修改规章制度信息
     * @param ruleRegulationDto
     * @return
     */
    @PostMapping("/update")
    public CommonResult update(@RequestBody RuleRegulationDto ruleRegulationDto) {
        int count = ruleRegulationService.updateRuleRegulation(ruleRegulationDto);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    /**
     * 删除规章制度信息
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    public CommonResult delete(@PathVariable("id") Integer id) {
        int count = ruleRegulationService.deleteRuleRegulation(id);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    /**
     * 上传文件
     * @param file
     * @return
     */
    @PostMapping("/upload")
    public CommonResult uploading(MultipartFile file) {
        return CommonResult.success(cosFileService.uploadImg(file));

    }

    /**
     * 批量删除规章制度信息
     * @param ids
     * @return
     */
    @PostMapping("/deleteList")
    public CommonResult deleteList(@RequestBody Integer[] ids) {
        int count = ruleRegulationService.deleteList(ids);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

}
